package com.elminster.data.import0.main;

import java.util.List;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.elminster.dao.impl.CatalogDaoImpl;
import com.elminster.dao.impl.DetailDaoImpl;
import com.elminster.data.import0.IDataImport;
import com.elminster.data.import0.ds.ExpensesDataStructure;
import com.elminster.entity.base.Catalog;
import com.elminster.entity.base.Detail;

public class ExpensesCsvImportMain {

    @SuppressWarnings("unchecked")
    public static void main(String[] args) throws Exception {
        String[] locations = {"applicationContext.xml"};
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(locations);
        
        String filePath = "c:/myExpenses1111.csv";
        
        IDataImport dataImport = (IDataImport) ctx.getBean("expensesCsvImport");
        DetailDaoImpl detailDao = (DetailDaoImpl) ctx.getBean("detailDaoImpl");
        CatalogDaoImpl catalogDao = (CatalogDaoImpl) ctx.getBean("catalogDaoImpl");
        List<ExpensesDataStructure> list = (List<ExpensesDataStructure>) dataImport.import0(filePath);
        for (int i = 1; i < list.size(); i++) {
            ExpensesDataStructure ds = list.get(i);
            Catalog catalog;
            List<Catalog> catalogList = catalogDao.findByName(ds.getLabel());
            if (catalogList.isEmpty()) {
                catalog = new Catalog(ds.getLabel());
                catalogDao.save(catalog);
            } else {
                catalog = catalogList.get(0);
            }
            
            Detail detail = new Detail(catalog, 1, ds.getDate(), ds.getAmount(), ds.getDate());
            detailDao.save(detail);
        }
        ctx.destroy();
    }
}
